<!--

-->
<template>
    <vxe-modal v-model="visible" :position="{top: '60px'}" :title="title" destroy-on-close :min-width="layout.modalMinWidth(600)" resize show-footer show-maximize :width="layout.modalWidth(600)" :height="layout.modalHeight()" @close="cancelClick">
        <el-form ref="ELForm" v-loading="isLoading" :model="state.model" :rules="rules" :validate-on-rule-change="false" class="ELForm" inline label-width="120px">
            <el-form-item :style="layout.col1" label="组织类型" prop="orgType">
                <el-radio-group v-model="state.model.orgType">
                    <el-radio-button :value="1">公司</el-radio-button>
                    <el-radio-button :value="2">部门</el-radio-button>
                </el-radio-group>
            </el-form-item>
            <el-form-item :style="layout.col1" label="上级组织" prop="pid">
                <el-tree-select v-model="state.model.pid" :check-strictly="true" :data="state.tree" :props="{label: 'name', children: 'children'}" :render-after-expand="false" style="width: 100%;" value-key="id"/>
            </el-form-item>
<!--            <el-form-item :style="layout.col1" label="所属区域" prop="areaCode">-->
<!--                <el-tree-select v-model="state.model.areaCode" :check-strictly="true" :data="state.areaTree" :props="{label: 'name', children: 'children'}" :render-after-expand="false" style="width: 100%;" value-key="code"/>-->
<!--            </el-form-item>-->
            <el-form-item :style="layout.col1" label="名称" prop="name">
                <el-input v-model="state.model.name"/>
            </el-form-item>
            <el-form-item :style="layout.col1" label="排序" prop="sort">
                <el-input-number v-model="state.model.sort" controls-position="right"/>
            </el-form-item>
            <el-form-item :style="layout.col1" label="备注" prop="remark">
                <el-input v-model="state.model.remark" type="textarea"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="cancelClick">取消</el-button>
            <el-button :loading="isSubmitting" type="primary" @click="beforeSubmit">
                确定
            </el-button>
        </template>
    </vxe-modal>
</template>

<script setup>
import {ref, reactive, computed, watch, onMounted} from "vue";
import {FormMixins} from "@/utils/FormMixins";
import * as api from "./api.js";

const {ELForm, visible, title, layout, isLoading, isSubmitting, state, setOptions, submitSuccess, cancelClick} = FormMixins();
const {$message} = window;

const Model = () => {
    return {
        id: 0,
        pid: undefined,
        orgType: 1,
        sort: 0,
        remark: undefined,
        status: 0,
        name: undefined,
        areaCode: undefined,
    }
}

// 校验规则
const rules = computed(() => {
    return {
        name: [{required: true, message: "请输入名称"}],
        // areaCode: [{required: state.model.orgType===1, message: "请选择所属区域"}],
        //pid: [{required: state.model.orgType > 1, validator: customRules.pid}],
    }
})

// 自定义校验规则
const customRules = {
    pid: (rule, value, callback) => {
        if (state.model.orgType > 1) {
            if (!value) return callback(new Error("请选择上级组织"));
        }
        if (state.model.id === value) {
            return callback(new Error("上级组织不能使用当前组织"));
        }
        return true;
    },
};

function typeChange() {
    state.model.pid = 0;
}

// 表单显示
async function show(config) {
    setOptions(config);
    state.model = Model();
    if (state.options) {
        state.model = {...state.model, ...state.options}
    }
    // 初始化数据
    const {success, data} = await api.GetOrgTreeList();
    if (!success) return 0;
    state.tree = data;

    // 查询区域
    // api.GetAreaTree().then(({success, data}) => {
    //     if (!success) return;
    //     state.areaTree = data;
    //     state.areaTree.treeMap(item => {
    //         // 科室不允许作为上级
    //         item.disabled = item.orgType === 2;
    //         return item;
    //     })
    // })

}

// 校验表单
function beforeSubmit() {
    ELForm.value.validate(isOk => {
        isOk && submit()
    })
}

// 提交表单
async function submit() {
    isSubmitting.value = true
    console.log("submit: ", state.model)
    const body = {...state.model}
    const {success, data} = await api[state.model.id ? 'UpdateOrgInfo' : 'AddOrgInfo'](body);
    isSubmitting.value = false
    if (!success) return 0;
    $message.success("操作成功")
    submitSuccess()
}

onMounted(() => {
})
defineExpose({
    show
})
</script>

<style lang="scss" scoped>
.EditForm {
}
</style>
